<?php
class Synthesis_Workbench_Block_Controls extends Mage_Core_Block_Template
{
	const DESIGN_TEMPLATE = 'workbench/controls.phtml';
	
	public function __construct()
	{
		if(Mage::getStoreConfig('dev/workbench/enabled') && $this->helper('core')->isDevAllowed()){
			$this->setTemplate(self::DESIGN_TEMPLATE);
		}
	}
	
	public function getErrors()
	{
		$errors = array();
		$errors['system'] = false;
		$msgStack = array();
		
        $file = Mage::getStoreConfig('dev/log/file');

		$logDir  = Mage::getBaseDir('var') . DS . 'log';
		$logFile = $logDir . DS . $file;	
	
		if($this->haveErrors($logFile))
		{
			$errors['messages']['system_log_exists'] =$this->__('System log exists');
			$errors['system'] = true;
			$errors['system_file_size'] = $this->helper('workbench')->FileSize($logFile);
			
			if($_COOKIE['system_filesize'] != filesize($logFile)){		
				$errors['messages']['new_error_occured'] = $this->__('New error occured during this session!');
				setcookie('system_filesize', filesize($logFile), 0, '/');
			}
			
			return $errors;
		}
		
		return false;

	}
	
	public function haveErrors($file)
	{

		if(file_exists($file)){
			if(!isset($_COOKIE['system_filesize']))
				setcookie('system_filesize', filesize($file), 0, '/');
			return true;
		}
		
		return false;
	}
	
	public function getVersion()
	{
		$modules = Mage::getConfig()->getNode('modules')->children();
		$modulesArray = (array)$modules;
		 
		return $modulesArray['Synthesis_Workbench']->version;
	}

	public function varDirExists()
	{
		return file_exists(Mage::helper('workbench')->getCachePath());
	}	
}